/*
 * Copyright 2003-2005 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

using System;

using DynamicTypeMap = argot.DynamicTypeMap;
using TypeElement = argot.TypeElement;
using TypeException = argot.TypeException;
using TypeHelper = argot.TypeHelper;
using TypeInputStream = argot.TypeInputStream;
using TypeOutputStream = argot.TypeOutputStream;
using TypeLibrary = argot.TypeLibrary;
using BigEndianSignedInteger = argot.common.BigEndianSignedInteger;
using BigEndianUnsignedByte = argot.common.BigEndianUnsignedByte;
using U8Boolean = argot.common.U8Boolean;


namespace argot.network
{
	
	public class TypeServer 
	:TypeLink
	{

		private ProtocolTypeMap _typeMap;
		private DynamicTypeMap _refMap;
		private TypeLibrary _library;	
		
		public TypeServer(TypeLibrary library, DynamicTypeMap refMap)
		{
			_typeMap = new ProtocolTypeMap(library);
			_refMap = refMap;
			_library = library;
		}
		
		public virtual byte[] ProcessMessage(byte[] request)
		{
			
			try
			{
				System.IO.MemoryStream bais = new System.IO.MemoryStream(request);
				TypeInputStream inStream = new TypeInputStream(bais, _typeMap);
				System.Object o = inStream.ReadObject(BigEndianUnsignedByte.TYPENAME);
				int action = (int) ((System.Byte) o);
				
				if (action == ProtocolTypeMap.MAP)
				{
					return processMap(inStream);
				}
				else if (action == ProtocolTypeMap.MAPRES)
				{
					return processMapReserve(inStream);
				}
				else if (action == ProtocolTypeMap.MAPREV)
				{
					return processMapReverse(inStream);
				}

				
				// return an error array.
				System.IO.MemoryStream baos = new System.IO.MemoryStream();
				TypeOutputStream sout = new TypeOutputStream(baos, _typeMap);
				sout.WriteObject("u8", (System.Object) ProtocolTypeMap.ERROR);
				baos.Flush();
				baos.Close();
				return baos.ToArray();
			}
			catch (TypeException)
			{
				throw new System.IO.IOException("exception reading data");
			}
		}
		
		/*
		* MapReverse is the situation where an identifier has
		* already been mapped on the server but the client doesn't
		* know about it.  The client sends the id and recieves
		* the type name and type definition.
		*/
		private byte[] processMapReverse(TypeInputStream in_Renamed)
		{
			System.Int32 id = (System.Int32) in_Renamed.ReadObject(BigEndianSignedInteger.TYPENAME);
			
			System.IO.MemoryStream baos = new System.IO.MemoryStream();
			TypeOutputStream sout = new TypeOutputStream(baos, _typeMap);
			
			System.String name = _refMap.GetName(id);
			TypeElement struct_Renamed = _refMap.GetStructure(id);
			
			byte[] definition = TypeHelper.ToByteArray(_refMap, struct_Renamed);
			
			sout.WriteObject("u8", (System.Object) ProtocolTypeMap.MAPREV);
			sout.WriteObject("u8ascii", name);
			sout.WriteObject("u16binary", definition);
			
			baos.Flush();
			baos.Close();
			
			return baos.ToArray();
		}
		
		/*
		* MapReserve is for situations where the client is attempting
		* to resolve a type which includes its own definition.  To
		* resolve this situation it reserves an ID for the type by
		* sending the name and receiving an ID.  The server may not
		* have the type and return -1.
		*/
		private byte[] processMapReserve(TypeInputStream in_Renamed)
		{
			System.String name = (System.String) in_Renamed.ReadObject("u8ascii");
			
			System.IO.MemoryStream baos = new System.IO.MemoryStream();
			TypeOutputStream sout = new TypeOutputStream(baos, _typeMap);
			
			sout.WriteObject("u8", (System.Object) ProtocolTypeMap.MAPRES);
			
			// First see if we have a type of the same name.
			int systemId = _library.GetId(name);
			if (systemId == TypeLibrary.NOTYPE)
			{
				sout.WriteObject(BigEndianSignedInteger.TYPENAME, (System.Object) (- 1));
			}
			else
			{
				int id;
				
				// This will find the id and it is is not yet 
				// mapped it will
				id = _refMap.GetId(name);
				
				sout.WriteObject(BigEndianSignedInteger.TYPENAME, (System.Object) id);
			}
			
			baos.Flush();
			baos.Close();
			
			return baos.ToArray();
		}
		
		private byte[] processMap(TypeInputStream in_Renamed)
		{
			System.String name = (System.String) in_Renamed.ReadObject("u8ascii");
			byte[] def = (byte[]) in_Renamed.ReadObject("u16binary");
			
			// Use the type name to compare the definition that was sent.
			// If they match we can allocate a new id and return.  If
			// they don't match then we can't agree on the type definition
			// and will return an invalid id.
			
			System.IO.MemoryStream baos = new System.IO.MemoryStream();
			TypeOutputStream sout = new TypeOutputStream(baos, _typeMap);
			sout.WriteObject("u8", (System.Object) ProtocolTypeMap.MAP);
			
			// First see if we have a type of the same name.
			int systemId = _library.GetId(name);
			if (systemId == TypeLibrary.NOTYPE)
			{
				sout.WriteObject(BigEndianSignedInteger.TYPENAME, (System.Object) (- 1));
			}
			else
			{
				// Check the structures are the same.
				TypeElement struct_Renamed = _library.GetStructure(systemId);
				
				if (TypeHelper.StructureMatches(_refMap, struct_Renamed, def))
				{
					int id;
					
					id = _refMap.GetId(name);
					
					sout.WriteObject(BigEndianSignedInteger.TYPENAME, (System.Object) id);
				}
				else
				{
					// Return an invalid id.
					sout.WriteObject(BigEndianSignedInteger.TYPENAME, (System.Object) (- 1));
				}
			}
			
			baos.Flush();
			baos.Close();
			
			return baos.ToArray();
		}
	}
}